package com.cosmoplat.mom.iot.client.spring.boot.autoconfigure;

import com.cosmoplat.mom.iot.client.IotClient;
import com.cosmoplat.mom.iot.client.RestTemplateIotClient;
import com.cosmoplat.mom.iot.client.config.IotClientProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 * @author yotta
 * @date 2025/6/16
 */
@Configuration
@Slf4j
public class IotClientAutoConfiguration {

    @ConfigurationProperties(prefix ="cosmoplat.mom.iot")
    @Bean
    public IotClientProperties iotClientProperties(){
        return new IotClientProperties();
    }


    @Bean
    @ConditionalOnMissingBean
    public IotClient iotClient(IotClientProperties iotClientProperties) {
        RestTemplateIotClient restTemplateIotClient = new RestTemplateIotClient(iotClientProperties);
        try {
            restTemplateIotClient.auth();
        } catch (Exception e) {
            log.error("IotClient init error", e);
        }
        return restTemplateIotClient;
    }
}
